package com.module.base.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;

/**
 * @author bsnl_yanxuwen
 * @date 2020/6/18 12:27
 * Description :
 */
public class PhoneUtils {

    /**
     * 对手机号码进行格式化成***
     */
    public static String getTelFormat(String str) {
        String regex = "\\d{11}";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        while (m.find()) { //一定需要先查找再调用group获取电话号码
            String number = m.group();
            String maskNumber = number.substring(0, 3) + "****" + number.substring(7, number.length());
            str = str.replace(number, maskNumber);
        }
        return str;

    }

    /**
     * 对手机号码进行格式化成***
     * 分享专用
     */
    public static String getTelFormat2(String str) {
        if (StringUtils.isNotBlank(str)) {
            if (StringUtils.isNumeric(str)) {
                int length = str.length();
                if (length >= 11) {
                    str = str.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                } else {
                    str = StringUtils.replaceAll(str, "1[3|4|5|7|8][0-9]{9}", "**");
                }
            } else {
                str = StringUtils.replaceAll(str, "1[3|4|5|7|8][0-9]{9}", "**");
            }
            return StringUtils.replaceAll(str, "[A-Z0-9]{6}", "**");
        }
        return str;
    }
}
